Public Class frmForeignKeyGenretor

  Private Enum TableIndc
    SELF
    REFERENCE
  End Enum

  Public mIntTableId As Integer
  Public mIntRefTableId As Integer
  Public mStrTableName As String = String.Empty

  Private mObjFKGnrt As New clsDBScriptGnrt
  Private mIntColumnId As Integer
  Private mIntRefColumnId As Integer

  Private Sub frmForeignKeyGenretor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim lStrDBStyle As String = String.Empty
    Dim lArrDBStyle As String()

    If mPrRefreshConn() = False Then Exit Sub

    mFnReadValue("DatabaseStyle", "", "Oracle,SqlCE", lStrDBStyle, Application.StartupPath + "\" + DDL_TEMPLATE_FILE)
    lArrDBStyle = Split(Split(lStrDBStyle, Chr(0))(0), ",")
    For Each lStr As String In lArrDBStyle
      cmbDBType.Items.Add(lStr)
    Next
    cmbDBType.SelectedIndex = 0

    txtTableName.Text = mStrTableName
    cmdUpdate.Enabled = False

    If mStrTableName.Trim <> String.Empty Then
      PopulateColumns(mStrTableName.Trim, TableIndc.SELF)
    End If
  End Sub

  Private Sub cmdViewTables_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdViewTables.Click
    FrmDisplayGrid.sCallingPurpose = gENMU_PUEPOSE.FK_Tables
    FrmDisplayGrid.sCallingControl = "txtTableName"
    FrmDisplayGrid.ShowDialog()
    If txtRefTableName.Text.Trim = txtTableName.Text.Trim Then
      txtRefTableName.Text = String.Empty
      lstRefColumns.DataSource = Nothing
    End If
    PopulateFKDtls(txtTableName.Text.Trim)
  End Sub

  Private Sub cmdViewRefTables_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdViewRefTables.Click
    Dim lStrRefTableName As String = txtRefTableName.Text.Trim
    FrmDisplayGrid.sCallingPurpose = gENMU_PUEPOSE.FK_Tables
    FrmDisplayGrid.sCallingControl = "txtRefTableName"
    FrmDisplayGrid.ShowDialog()
    If txtRefTableName.Text.Trim = txtTableName.Text.Trim Then
      txtRefTableName.Text = lStrRefTableName
      MsgBox("Please select one different table with referencing table", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If
  End Sub

  Private Sub txtTableName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTableName.TextChanged
    PopulateColumns(txtTableName.Text.Trim, TableIndc.SELF)
    PopulateFKDtls(txtTableName.Text.Trim)
  End Sub

  Private Sub txtRefTableName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRefTableName.TextChanged
    PopulateColumns(txtRefTableName.Text.Trim, TableIndc.REFERENCE)
  End Sub

  Private Sub dgvFK_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvFK.CellClick

    If e.RowIndex = -1 Then
      Exit Sub
    End If

    If e.ColumnIndex = 0 Then

      If MsgBox("Delete Foreign Key " & dgvFK.Rows(e.RowIndex).Cells("clmFKName").Value & "?", MsgBoxStyle.YesNo, gCONST_MSG_HEADER) = MsgBoxResult.Yes Then
        dgvFK.Rows.RemoveAt(e.RowIndex)
        dgvFK.ClearSelection()
        cmdUpdate.Enabled = False
      End If
   
    Else
      Call dgvFK_SelectionChanged(sender, New System.EventArgs)
    End If
  End Sub

  Private Sub dgvFK_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvFK.SelectionChanged
   
    If dgvFK.SelectedRows.Count <> 0 And dgvFK.RowCount <> 0 Then
      If dgvFK.CurrentCell.ColumnIndex <> 0 Then

        txtRefTableName.Text = dgvFK.SelectedRows.Item(0).Cells("clmRefTable").Value
        PopulateColumns(txtRefTableName.Text.Trim, TableIndc.REFERENCE)
        lstColumns.Text = dgvFK.SelectedRows.Item(0).Cells("clmColumn").Value
        lstRefColumns.Text = dgvFK.SelectedRows.Item(0).Cells("clmRefColumn").Value
        mIntTableId = dgvFK.SelectedRows.Item(0).Cells("clmTableId").Value
        mIntRefTableId = dgvFK.SelectedRows.Item(0).Cells("clmRefTableId").Value
        mIntColumnId = dgvFK.SelectedRows.Item(0).Cells("clmClmId").Value
        mIntRefColumnId = dgvFK.SelectedRows.Item(0).Cells("clmRefClmId").Value

        cmdUpdate.Enabled = True
      End If
    End If
  End Sub

  Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
    Dim lStrTableName As String = String.Empty
    Dim lStrColumnName As String = String.Empty
    Dim lStrRefTableName As String = String.Empty
    Dim lStrRefColumnName As String = String.Empty

    If Len(txtTableName.Text.Trim) = 0 Then
      MsgBox("Please select one table", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Else
      lStrTableName = txtTableName.Text.Trim
    End If

    If Len(lstColumns.Text.Trim) = 0 Then
      MsgBox("Please select one column", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Else
      lStrColumnName = lstColumns.Text.Trim
    End If

    If Len(txtRefTableName.Text.Trim) = 0 Then
      MsgBox("Please select one reference table", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Else
      lStrRefTableName = txtRefTableName.Text.Trim
    End If

    If Len(lstRefColumns.Text.Trim) = 0 Then
      MsgBox("Please select one reference column", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Else
      lStrRefColumnName = lstRefColumns.Text.Trim
    End If

    If mObjFKGnrt.IsPK(mIntRefTableId, mObjFKGnrt.GetColmunId(lStrRefColumnName, mIntRefTableId)) = False Then
      MsgBox("The referenced columns must be primary key", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If

    For Each lObjRow As DataGridViewRow In dgvFK.Rows
      If lObjRow.Cells("clmColumn").Value = lStrColumnName _
      And lObjRow.Cells("clmRefTable").Value = lStrRefTableName _
      And lObjRow.Cells("clmRefColumn").Value = lStrRefColumnName Then
        If dgvFK.SelectedRows.Count <> 0 AndAlso lObjRow.Index <> dgvFK.SelectedRows(0).Index Then
          MsgBox("This foreign key already exists", MsgBoxStyle.Information, gCONST_MSG_HEADER)
          Exit Sub
        End If
      End If
    Next

    mIntColumnId = mObjFKGnrt.GetColmunId(lStrColumnName, mIntTableId)
    mIntRefColumnId = mObjFKGnrt.GetColmunId(lStrRefColumnName, mIntRefTableId)

    If dgvFK.SelectedRows.Count <> 0 Then
      With dgvFK.SelectedRows(0)
        .Cells("clmTable").Value = lStrTableName
        .Cells("clmColumn").Value = lStrColumnName
        .Cells("clmRefTable").Value = lStrRefTableName
        .Cells("clmRefColumn").Value = lStrRefColumnName
        .Cells("clmTableId").Value = mIntTableId
        .Cells("clmClmId").Value = mIntColumnId
        .Cells("clmRefTableId").Value = mIntRefTableId
        .Cells("clmRefClmId").Value = mIntRefColumnId
      End With
    Else
      Call cmdAdd_Click(New Object, New System.EventArgs)
    End If
  End Sub

  Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
    Dim lStrTableName As String = String.Empty
    Dim lStrColumnName As String = String.Empty
    Dim lStrRefTableName As String = String.Empty
    Dim lStrRefColumnName As String = String.Empty
    Dim lIntFKId As Integer
    Dim lIntFKIndexId As Integer
    Dim lIntNewRowIndex As Integer

    If Len(txtTableName.Text.Trim) = 0 Then
      MsgBox("Please select one table", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Else
      lStrTableName = txtTableName.Text.Trim
    End If

    If Len(lstColumns.Text.Trim) = 0 Then
      MsgBox("Please select one column", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Else
      lStrColumnName = lstColumns.Text.Trim
    End If

    If Len(txtRefTableName.Text.Trim) = 0 Then
      MsgBox("Please select one reference table", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Else
      lStrRefTableName = txtRefTableName.Text.Trim
    End If

    If Len(lstRefColumns.Text.Trim) = 0 Then
      MsgBox("Please select one reference column", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Else
      lStrRefColumnName = lstRefColumns.Text.Trim
    End If

    If mObjFKGnrt.IsPK(mIntRefTableId, mObjFKGnrt.GetColmunId(lStrRefColumnName, mIntRefTableId)) = False Then
      MsgBox("The referenced columns must be primary key", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If

    For Each lObjRow As DataGridViewRow In dgvFK.Rows
      If lObjRow.Cells("clmColumn").Value = lStrColumnName _
      And lObjRow.Cells("clmRefTable").Value = lStrRefTableName _
      And lObjRow.Cells("clmRefColumn").Value = lStrRefColumnName Then
        MsgBox("This foreign key already exists", MsgBoxStyle.Information, gCONST_MSG_HEADER)
        Exit Sub
      End If
    Next

    If dgvFK.RowCount = 0 Then
      lIntFKId = mObjFKGnrt.GetMaxFKId() + 1
      lIntFKIndexId = 1
    Else
      lIntFKId = dgvFK.Rows(0).Cells("clmFKId").Value
    End If

    For Each lObjRow As DataGridViewRow In dgvFK.Rows
      If lObjRow.Cells("clmFKIndexId").Value >= lIntFKIndexId Then
        lIntFKIndexId = lObjRow.Cells("clmFKIndexId").Value + 1
      End If
    Next

    mIntColumnId = mObjFKGnrt.GetColmunId(lStrColumnName, mIntTableId)
    mIntRefColumnId = mObjFKGnrt.GetColmunId(lStrRefColumnName, mIntRefTableId)

    lIntNewRowIndex = dgvFK.Rows.Add()
    With dgvFK.Rows(lIntNewRowIndex)
      .Cells("clmFKName").Value = "FK_" & mObjFKGnrt.GetTableShrtName(mIntTableId) & "_" & lIntFKIndexId & mIntColumnId & mIntRefColumnId
      .Cells("clmTable").Value = lStrTableName
      .Cells("clmColumn").Value = lStrColumnName
      .Cells("clmRefTable").Value = lStrRefTableName
      .Cells("clmRefColumn").Value = lStrRefColumnName
      .Cells("clmFKId").Value = lIntFKId
      .Cells("clmFKIndexId").Value = lIntFKIndexId
      .Cells("clmTableId").Value = mIntTableId
      .Cells("clmClmId").Value = mIntColumnId
      .Cells("clmRefTableId").Value = mIntRefTableId
      .Cells("clmRefClmId").Value = mIntRefColumnId
    End With
    dgvFK.Rows(lIntNewRowIndex).Cells(1).Selected = True
    Call dgvFK_SelectionChanged(dgvFK, New System.EventArgs)
  End Sub

  Private Sub cmdCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCommit.Click
    Dim lStrQuery As String = String.Empty
    Dim lIntNumOfRefPK As Integer
    Dim lIntNumOfRefClms As Integer
    Dim lIntNumOfClms As Integer
    Dim lArrUpdateQuery As New ArrayList
    Dim lIntTableId As Integer = mIntTableId

    If Len(txtTableName.Text.Trim) = 0 Then
      MsgBox("Please select one table", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If

    'Validate
    For Each lObjRow As DataGridViewRow In dgvFK.Rows
      lIntNumOfRefPK = mObjFKGnrt.GetPKCount(lObjRow.Cells("clmRefTableId").Value)
      lIntNumOfRefClms = 0
      For Each lObjCmpRow As DataGridViewRow In dgvFK.Rows
        If lObjCmpRow.Cells("clmRefTableId").Value = lObjRow.Cells("clmRefTableId").Value Then
          lIntNumOfRefClms += 1
        End If
      Next
      If lIntNumOfRefPK <> lIntNumOfRefClms Then
        MsgBox("No matching unique or primary key for this column list of referenced table - " & lObjRow.Cells("clmRefTable").Value, MsgBoxStyle.Information, gCONST_MSG_HEADER)
        Exit Sub
      End If
    Next

    lIntNumOfClms = 0
    lIntNumOfRefClms = 0
    For i As Integer = 0 To dgvFK.Rows.Count - 1
      For j As Integer = 0 To dgvFK.Rows.Count - 1
        If i <> j Then
          If dgvFK.Rows(i).Cells("clmTableId").Value = dgvFK.Rows(j).Cells("clmTableId").Value _
          And dgvFK.Rows(i).Cells("clmRefTableId").Value = dgvFK.Rows(j).Cells("clmRefTableId").Value Then
            If dgvFK.Rows(i).Cells("clmClmId").Value <> dgvFK.Rows(j).Cells("clmClmId").Value Then
              lIntNumOfClms += 1
            End If
            If dgvFK.Rows(i).Cells("clmRefClmId").Value <> dgvFK.Rows(j).Cells("clmRefClmId").Value Then
              lIntNumOfRefClms += 1
            End If
          End If
        End If
      Next
      If lIntNumOfClms <> lIntNumOfRefClms Then
        MsgBox("Number of referencing columns must matching referenced columns", MsgBoxStyle.Information, gCONST_MSG_HEADER)
        Exit Sub
      End If
    Next

    'Save
    mObjFKGnrt.RemoveFK(lIntTableId)
    For Each lObjRow As DataGridViewRow In dgvFK.Rows
      With lObjRow
        lStrQuery = "INSERT INTO TRDX_FRGN_KEY_DTLS (TFK_TABLE_ID,TFK_COLUMN_ID,TFK_FRGN_KEY_ID,TFK_FRGN_KEY_INDX_ID,TFK_RFRNCE_TABLE_ID,TFK_RFRNCE_COLUMN_ID,TFK_TABLE_NAME,TFK_COLUMN_NAME,TFK_RFRNCE_TABLE_NAME,TFK_RFRNCE_COLUMN_NAME) VALUES (" & _
                     .Cells("clmTableId").Value & "," & _
                     .Cells("clmClmId").Value & "," & _
                     .Cells("clmFKId").Value & "," & _
                     .Cells("clmFKIndexId").Value & "," & _
                     .Cells("clmRefTableId").Value & "," & _
                     .Cells("clmRefClmId").Value & "," & _
                     "'" & .Cells("clmTable").Value.ToString & "'," & _
                     "'" & .Cells("clmColumn").Value.ToString & "'," & _
                     "'" & .Cells("clmRefTable").Value.ToString & "'," & _
                     "'" & .Cells("clmRefColumn").Value.ToString & "')"
      End With
      lArrUpdateQuery.Add(lStrQuery)
    Next

    If mObjFKGnrt.Update(lArrUpdateQuery) = True Then
      PopulateFKDtls(txtTableName.Text.Trim)
      MsgBox("Save successfully", MsgBoxStyle.Information, gCONST_MSG_HEADER)
    Else
      MsgBox("Save failed", MsgBoxStyle.Information, gCONST_MSG_HEADER)
    End If
  End Sub

  Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
    Dim lArrScript As New ArrayList
    Dim lStrScript As String
    Dim lIntScriptIndex As Integer
    Dim lStrFileName As String = "FK_" + txtTableName.Text.Trim + ".sql"
    Dim lStrScriptTemplate As String
    Dim lIntRefTableId As Integer
    Dim lHstRefTablsIds As New Hashtable

    lHstRefTablsIds.Clear()
    lStrScriptTemplate = ReadTemplateScript(cmbDBType.Text.Trim, TM_CREATE_FK)
    For Each lObjRow As DataGridViewRow In dgvFK.Rows

      lIntRefTableId = lObjRow.Cells("clmRefTableId").Value
      'If lHstRefTablsIds.Contains(lIntRefTableId) Then
      '  lIntScriptIndex = lHstRefTablsIds(lIntRefTableId)
      '  lStrScript = lArrScript.Item(lIntScriptIndex)
      'Else
      lStrScript = lStrScriptTemplate
      lIntScriptIndex = lArrScript.Add(lStrScript)
      'lHstRefTablsIds.Add(lIntRefTableId, lIntScriptIndex)
      'End If

      lStrScript = ReplaceParameter(lStrScript, TM_FK_NAME, lObjRow.Cells("clmFKName").Value.ToString)
      lStrScript = ReplaceParameter(lStrScript, TM_TABLE_NAME, lObjRow.Cells("clmTable").Value.ToString)
      lStrScript = ReplaceParameter(lStrScript, TM_REF_TABLE_NAME, lObjRow.Cells("clmRefTable").Value.ToString)
      lStrScript = ReplaceParameter(lStrScript, TM_START, String.Empty)
      lStrScript = ReplaceParameter(lStrScript, TM_COLUMN_NAME, lObjRow.Cells("clmColumn").Value.ToString)
      lStrScript = ReplaceParameter(lStrScript, TM_REF_COLUMN_NAME, lObjRow.Cells("clmRefColumn").Value.ToString)
      lArrScript.Item(lIntScriptIndex) = lStrScript
    Next

    lStrScript = String.Empty
    For Each lStr As String In lArrScript
      lStr = ReplaceParameter(lStr, TM_END, String.Empty)
      lStr = ReplaceParameter(lStr, TM_REMOVE, String.Empty)
      lStr = ReplaceSepcChar(lStr)
      lStrScript = lStrScript + lStr + vbCrLf
    Next

    If GenerateScript(lStrFileName, lStrScript) = True Then
      MsgBox("Save as '" & lStrFileName & "' successfully", MsgBoxStyle.Information, gCONST_MSG_HEADER)
    Else
      MsgBox("Save failed", MsgBoxStyle.Information, gCONST_MSG_HEADER)
    End If

  End Sub

  Private Sub cmdGenerateAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerateAll.Click
    Dim lArrScript As New ArrayList
    Dim lStrScript As String
    Dim lObjFilePath As String = String.Empty
    Dim lStrFileName As String
    Dim lStrScriptTemplate As String
    Dim objTags As New clsTags
    Dim lObjDataTable As DataTable
    Dim lObjArrRow() As DataRow
    Dim lIntRefTableId As Integer
    Dim lHstRefTablsIds As New Hashtable
    Dim lIntScriptIndex As Integer

    Dim lStrTableName As String = String.Empty

    Try

      lStrScriptTemplate = ReadTemplateScript(cmbDBType.Text.Trim, TM_CREATE_FK)

      If SelectFolder(lObjFilePath) = False Then
        Exit Sub
      End If

      mPrRefreshConn()
      objTags.fnSelectAllTables()
      lObjDataTable = mObjFKGnrt.PopulateAllFK()
      While objTags.mDrTags.Read()
        lArrScript.Clear()
        lStrTableName = objTags.mDrTags("TABM_TABLE_NAME").ToString

        lObjArrRow = lObjDataTable.Select("TFK_TABLE_NAME='" & lStrTableName & "'")

        If lObjArrRow.Length <> 0 Then

          lHstRefTablsIds.Clear()
          For Each lObjRow As DataRow In lObjArrRow

            lIntRefTableId = lObjRow("TFK_RFRNCE_TABLE_ID")
            'If lHstRefTablsIds.Contains(lIntRefTableId) Then
            '  lIntScriptIndex = lHstRefTablsIds(lIntRefTableId)
            '  lStrScript = lArrScript.Item(lIntScriptIndex)
            'Else
            lStrScript = lStrScriptTemplate
            lIntScriptIndex = lArrScript.Add(lStrScript)
            'lHstRefTablsIds.Add(lIntRefTableId, lIntScriptIndex)
            'End If

            lStrScript = ReplaceParameter(lStrScript, TM_FK_NAME, "FK_" & mObjFKGnrt.GetTableShrtName(lObjRow("TFK_TABLE_ID")) & "_" & mObjFKGnrt.GetTableShrtName(lObjRow("TFK_RFRNCE_TABLE_ID")))
            lStrScript = ReplaceParameter(lStrScript, TM_TABLE_NAME, lObjRow("TFK_TABLE_NAME").ToString)
            lStrScript = ReplaceParameter(lStrScript, TM_REF_TABLE_NAME, lObjRow("TFK_RFRNCE_TABLE_NAME").ToString)
            lStrScript = ReplaceParameter(lStrScript, TM_START, String.Empty)
            lStrScript = ReplaceParameter(lStrScript, TM_COLUMN_NAME, lObjRow("TFK_COLUMN_NAME").ToString)
            lStrScript = ReplaceParameter(lStrScript, TM_REF_COLUMN_NAME, lObjRow("TFK_RFRNCE_COLUMN_NAME").ToString)
            lArrScript.Item(lIntScriptIndex) = lStrScript
          Next
        End If

        lStrScript = String.Empty
        For Each lStr As String In lArrScript
          lStr = ReplaceParameter(lStr, TM_END, String.Empty)
          lStr = ReplaceParameter(lStr, TM_REMOVE, String.Empty)
          lStr = ReplaceSepcChar(lStr)
          lStrScript = lStrScript + lStr + vbCrLf
        Next

        lStrFileName = "FK_" + lStrTableName + ".sql"
        If Len(lStrScript.Trim) <> 0 Then
          WriteFile(lObjFilePath, lStrFileName, lStrScript)
        End If
      End While

      MsgBox("Save in folder - " & lObjFilePath & " successfully", MsgBoxStyle.Information, gCONST_MSG_HEADER)
    Catch ex As Exception
      MsgBox("Save failed", MsgBoxStyle.Information, gCONST_MSG_HEADER)
    End Try

  End Sub

  '--------------------------------------------------------------------------------------------------------------------'

  Private Sub PopulateColumns(ByVal aStrTableName As String, ByVal aEnmIndc As TableIndc)

    If Len(txtTableName.Text.Trim) = 0 Then
      MsgBox("Please select one table", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If

    If aEnmIndc = TableIndc.SELF Then
      mIntTableId = mObjFKGnrt.GetTableId(aStrTableName.Trim)
      lstColumns.DataSource = mObjFKGnrt.PopulateColmuns(aStrTableName.Trim)
      lstColumns.ClearSelected()
    Else
      mIntRefTableId = mObjFKGnrt.GetTableId(aStrTableName.Trim)
      lstRefColumns.DataSource = mObjFKGnrt.PopulateColmuns(aStrTableName.Trim)
      lstRefColumns.ClearSelected()
    End If
  End Sub

  Private Sub PopulateFKDtls(ByVal aStrTableName As String)
    Dim lObjDataReader As OracleClient.OracleDataReader
    Dim lIntTableId As Integer
    Dim lStrTableShrtName As String = String.Empty
    Dim lIntrowIndex As Integer

    lIntTableId = mObjFKGnrt.GetTableId(aStrTableName.Trim)
    lStrTableShrtName = mObjFKGnrt.GetTableShrtName(lIntTableId)
    lObjDataReader = mObjFKGnrt.PopulateFK(lIntTableId)
    If IsNothing(lObjDataReader) = False Then
      dgvFK.Rows.Clear()
      While lObjDataReader.Read

        lIntrowIndex = dgvFK.Rows.Add()
        With dgvFK.Rows(lIntrowIndex)
          .Cells("clmFKId").Value = lObjDataReader.Item("TFK_FRGN_KEY_ID")
          .Cells("clmFKName").Value = "FK_" & lStrTableShrtName & "_" & lObjDataReader.Item("TFK_FRGN_KEY_INDX_ID") & lObjDataReader.Item("TFK_COLUMN_ID") & lObjDataReader.Item("TFK_RFRNCE_COLUMN_ID")
          .Cells("clmTable").Value = lObjDataReader.Item("TFK_TABLE_NAME")
          .Cells("clmColumn").Value = lObjDataReader.Item("TFK_COLUMN_NAME")
          .Cells("clmRefTable").Value = lObjDataReader.Item("TFK_RFRNCE_TABLE_NAME")
          .Cells("clmRefColumn").Value = lObjDataReader.Item("TFK_RFRNCE_COLUMN_NAME")
          .Cells("clmFKIndexId").Value = lObjDataReader.Item("TFK_FRGN_KEY_INDX_ID")
          .Cells("clmTableId").Value = lObjDataReader.Item("TFK_TABLE_ID")
          .Cells("clmClmId").Value = lObjDataReader.Item("TFK_COLUMN_ID")
          .Cells("clmRefTableId").Value = lObjDataReader.Item("TFK_RFRNCE_TABLE_ID")
          .Cells("clmRefClmId").Value = lObjDataReader.Item("TFK_RFRNCE_COLUMN_ID")
        End With

      End While
      dgvFK.ClearSelection()
    End If

  End Sub

End Class